﻿
//生成题库并获取
function GetNewExam(id, t) {
    if (t == null || t < 1) {
        t = 1;
    }
    $("#titleid").html("第" + t + "题");
    $("#hidQuestionID").val(t);
    $("#hidExamRoomID").val(id);
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "GetNewExam", id: id },
        success: function (result) {
            GetExamInfo(result, t);
        }
    });
}

//清除题库
function DelNewExam(id) {
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "DelNewExam", id: id },
        success: function (result) {
            window.location.href = window.location.href;
        }
    });
}

//获取信息
function GetExamInfo(jsonData, t) {
    var dataObj = jsonData;  //eval("(" + jsonData + ")"); //转换为json对象
    var strHtml = "";
    var gongjitishu = 100;
    var danqianyongshi = "0秒";
    if (dataObj != null) {
        var ifsubmit = dataObj["ifsubmit"];
        $("#hidIfSubmit").val(ifsubmit);
        var list = dataObj["list"];
        GetNumberHtml(list, t);
        gongjitishu = list.length;
        for (var i = 0; i < list.length; i++) {
            var cid = list[i]["id"];
            var ifcollect = list[i]["ifcollect"];
            if (t == cid) {
                $("#hidFavorite").val(ifcollect);
                if (ifcollect == "0") {
                    $("#favorite").html("<img src=\"images/sers/collection_no.jpg\" />未收藏");
                }
                else {
                    $("#favorite").html("<img src=\"images/sers/collection.jpg\" />已收藏");
                }
                strHtml += "<ul><li class=\"dc1_queitem\"><h2 class=\"blue\"><span class=\"dc1_quen\"></span>";
                var title = list[i]["title"];
                var usetime = list[i]["usetime"];
                if (usetime != null && usetime != "") {
                    danqianyongshi = usetime + "秒";
                }
                var notebook = list[i]["notebook"];
                var errorbook = list[i]["errorbook"];
                if (notebook != null && notebook != "") {
                    $("#txtNote").val(notebook);
                }
                else {
                    $("#txtNote").val("");
                }
                if (errorbook != null && errorbook != "") {
                    $("#txtError").val(errorbook);
                }
                else {
                    $("#txtError").val("");
                }
                var flog = list[i]["flog"];
                var question = list[i]["question"];
                var chooseanswer = list[i]["chooseanswer"];
                if (chooseanswer == "0") {
                    if (flog == 0) {
                        question = question + "(A)";
                    }
                }
                if (chooseanswer == "1") {
                    if (flog == 0) {
                        question = question + "(B)";
                    }
                }
                if (chooseanswer == "2") {
                    question = question + "(C)";
                }
                if (chooseanswer == "3") {
                    question = question + "(D)";
                }
                if (ifcollect == 0) {
                    //strHtml += "<li><div style=\"float: left; width: 24px; height: 26px; background-image: url(../images/favorite.JPG);display: none;\"></div>" + question + "</li>";
                }
                else {
                    //strHtml += "<li><div style=\"float: left; width: 24px; height: 26px; background-image: url(../images/favorite.JPG);\"></div>" + question + "</li>";
                }
                strHtml += question + "</h2><div class=\"dc1_queoption\" id=\"div3\">";
                var answerImg = "";
                var answer = list[i]["answer"];
                for (var j = 0; j < answer.length; j++) {
                    if (flog == 0) {
                        strHtml += answer[j] + "<br />";
                        answerImg += "<img src=\"images/sers/result_" + j + ".jpg\" alt=\"a\" onclick=\"Answer(" + j + ");\" style=\"cursor: pointer;\" />&nbsp;";
                    }
                    if (flog == 1) {
                        if (chooseanswer == "0" && j == 0) {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" checked=\"checked\" onclick=\"Answer(" + j + ");\" />" + answer[j] + "<br />";
                        }
                        else if (chooseanswer == "1" && j == 1) {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" checked=\"checked\" onclick=\"Answer(" + j + ");\" />" + answer[j] + "<br />";
                        }
                        else {
                            strHtml += "<input name=\"chooseanswer\" type=\"radio\" onclick=\"Answer(" + j + ");\" />" + answer[j] + "<br />";
                        }
                    }
                }
                strHtml += "</div></li></ul>";

            }
        }
    }

    $("#gongjitishu").html(gongjitishu);
    $("#danqianyongshi").html(danqianyongshi);
    $("#answers").html(answerImg);
    $("#question").html(strHtml);
}

//是否自动答题
function AutoAnswer() {
    var hidAutoAnswer = $("#hidAutoAnswer").val();
    if (hidAutoAnswer == "0") {
        $("#hidAutoAnswer").val("1");
    }
    else {
        $("#hidAutoAnswer").val("0");
    }
}

//题目数字
function GetNumberHtml(list, t) {
    var dangqiantihao = t;
    var yidatishu = 0;
    var hidAutoAnswer = $("#hidAutoAnswer").val();

    var titleNumber = "";
    for (var i = 0; i < list.length; i++) {
        var cid = list[i]["id"];
        if (i != 0 && i % 25 == 0) {
            titleNumber += "<div class=\"clearok\"></div>";
        }
        var chooseanswer = list[i]["chooseanswer"];
        if (chooseanswer != null && chooseanswer != "") {
            yidatishu++;
            if (t == cid) {
                titleNumber += "<div class=\"num over\">" + (i + 1) + "</div>";
            }
            else {
                titleNumber += "<div class=\"num out\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
            }
        }
        else {
            if (t == cid) {
                titleNumber += "<div class=\"num over\">" + (i + 1) + "</div>";
            }
            else {
                titleNumber += "<div class=\"num\" onclick=\"ChooseQuestion(" + (i + 1) + ");\">" + (i + 1) + "</div>";
            }
        }

    }
    $("#dangqiantihao").html(dangqiantihao);
    $("#yidatishu").html(yidatishu);
    $("#titleNumber").html(titleNumber);
}

//上一题下一题
function NextQuestion(t) {
    var chooseId = parseInt($("#hidQuestionID").val());
    if (t == -1) {
        if (chooseId > 1) {
            chooseId--;
        }
    }
    else {
        if (chooseId < 100) {
            chooseId++;
        }
    }

    $("#hidQuestionID").val(chooseId);
    var hidExamRoomID = $("#hidExamRoomID").val();
    GetNewExam(hidExamRoomID, chooseId);
}

//保存笔记
function SaveNote() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var note = $("#txtNote").val();
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Answer", id: id, cid: cid, notebook: note },
        success: function (result) {
            alert("保存成功");
            CloseContentDiv();
            GetNewExam(id, cid);
        }
    });
}

//保存错误反馈
function SaveError() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var errorbook = $("#txtError").val();
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Answer", id: id, cid: cid, errorbook: errorbook },
        success: function (result) {
            alert("保存成功");
            CloseContentDiv();
            GetNewExam(id, cid);
        }
    });
}

//查找题目
function Search() {
    var id = $("#hidExamRoomID").val();
    var title = $("#txtSearch").val();
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Search", id: id, title: encodeURI(title) },
        success: function (result) {
            if (result != null && result != "") {
                var cid = result[0]["id"];
                CloseContentDiv();
                GetNewExam(id, cid);
                $("#txtSearch").val("");
            }
        }
    });
}

//选择题号
function ChooseQuestion(t) {
    var id = $("#hidExamRoomID").val();
    GetNewExam(id, t);
}

//收藏
function Favorite() {
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    var t = $("#hidFavorite").val();
    if (t == "0") {
        t = 1;
    }
    else {
        t = 0;
    }
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Answer", id: id, cid: cid, ifcollect: t },
        success: function (result) {
            if (t == 1) {
                alert("收藏成功");
                $("#favorite").html("<img src=\"images/sers/collection.jpg\" />已收藏");
            }
            else {
                alert("取消成功");
                $("#favorite").html("<img src=\"images/sers/collection_no.jpg\" />未收藏");
            }
            GetNewExam(id, cid);
        }
    });
}

//提交试卷
function SubmitAnswer() {
    var yidatishu = $("#yidatishu").html();
    var gongjitishu = $("#gongjitishu").html();
    if (yidatishu != gongjitishu) {
        var bln = confirm("还有未答的题目，交卷后不可修改，确定要交卷吗？")
        if (bln == false) {
            return;
        }
    }
    var ifsubmit = $("#hidIfSubmit").val();
    if (ifsubmit == "1") {
        alert("该试题已经提交，不可重复提交");
        return;
    }
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Answer", id: id, cid: cid, ifsubmit: 1 },
        success: function (result) {
            GetNewExam(id, 1);
            alert("交卷成功");
        }
    });
}

//答题
function Answer(t) {
    var ifsubmit = $("#hidIfSubmit").val();
    if (ifsubmit == "1") {
        alert("该试题已经提交，不可修改答案");
        return;
    }
    var id = $("#hidExamRoomID").val();
    var cid = $("#hidQuestionID").val();
    $.ajax({
        type: "POST",
        url: "Service.ashx",
        dataType: "json",
        data: { action: "Answer", id: id, cid: cid, chooseanswer: t },
        success: function (result) {
            var hidAutoAnswer = $("#hidAutoAnswer").val();
            if (hidAutoAnswer == "1" && parseInt(cid) < 100) {
                GetNewExam(id, parseInt(cid) + 1);
            }
            else {
                GetNewExam(id, cid);
            }
        }
    });
}